<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "baserole".
 *
 * @property int $Id 角色标识
 * @property int $Pid 上级标识
 * @property string $RoleName 角色名称
 * @property int $Nullity 启用状态
 * @property string $PathRoute 组别路径
 * @property string $Remarks 注释说明
 * @property int $GroupId 组织类别
 * @property string $HeadUrl 角色头像
 */
class Baserole extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'baserole';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['Pid', 'GroupId'], 'integer'],
            [['RoleName'], 'required'],
            [['RoleName'], 'string', 'max' => 30],
            //[['Nullity'], 'string', 'max' => 1],
            [['PathRoute'], 'string', 'max' => 1024],
            [['Remarks'], 'string', 'max' => 256],
            [['HeadUrl'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'Id' => 'ID',
            'Pid' => 'Pid',
            'RoleName' => 'Role Name',
            'Nullity' => 'Nullity',
            'PathRoute' => 'Path Route',
            'Remarks' => 'Remarks',
            'GroupId' => 'Group ID',
            'HeadUrl' => 'Head Url',
        ];
    }


    /**
     * 获取角色分类列表
     */
    public function getRoleList($groupId)
    {
        $data = self::find()->where(['GroupId' => $groupId])->asArray()->all();
        $list = [
            'data' => $data,
            'statusCode' => 200,
            'limit' => 10,
            'page' => 1,
            'total' => count($data),
            ];

        return $list;
    }


    public function insertData($data)
    {
        $role = new Baserole();
        $role->RoleName = $data['RoleName'];
        $role->Nullity = isset($data['Nullity']) ? 0 : 1;
        $role->Remarks = $data['Remarks'];
        $role->GroupId = $data['GroupId'];
        if ($role->save()) {
            return ['statusCode' => 200, 'message' => '角色信息提交成功'];
        }
        return ['statusCode' => 300, 'message' => '角色信息提交失败'];
    }


    public function updateData($data)
    {
        $role = self::findOne(['Id'=>$data['RoleId']]);
        $role->RoleName = $data['RoleName'];
        $role->Nullity = isset($data['Nullity']) ? 0 : 1;
        $role->Remarks = $data['Remarks'];
        $role->GroupId = $data['GroupId'];
        if ($role->save()) {
            return ['statusCode' => 200, 'message' => '角色信息提交成功'];
        }
        return ['statusCode' => 300, 'message' => '角色信息提交失败'];
    }


    public function deleteRole($data)
    {
        $id = $data['id'];
        $gid = $data['gid'];
        $query = self::deleteAll(['Id' => $id, 'GroupId' => $gid]);
        Baseprivilege::deleteAll(['RoleId' => $id]);
        if ($query){
            return ['statusCode'=>200, 'message'=>'已成功删除角色信息'];
        }
        return ['statusCode'=>300, 'message'=>'删除角色信息失败'];
    }
}
